package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.constant.WorkConstant;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.ZjWorkSpareReturn;
import com.zb.entity.ZjWorkSpareReturnMaterial;
import com.zb.entity.ZjWorkSpareReturnReceiptSend;
import com.zb.mapper.ZjWorkSpareReturnMapper;
import com.zb.service.ZjWorkSpareReturnMaterialService;
import com.zb.service.ZjWorkSpareReturnReceiptSendService;
import com.zb.service.ZjWorkSpareReturnService;
import com.zb.utils.ZbOrderNoUtil;
import com.zb.vo.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 工单备件旧件退回表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-10-30
 */
@Service
public class ZjWorkSpareReturnServiceImpl extends ServiceImpl<ZjWorkSpareReturnMapper, ZjWorkSpareReturn> implements ZjWorkSpareReturnService {
    @Resource
    private ZjWorkSpareReturnMapper workSpareReturnMapper;
    @Resource
    private ZjWorkSpareReturnReceiptSendService workSpareReturnReceiptSendService;
    @Resource
    private ZjWorkSpareReturnMaterialService workSpareReturnMaterialService;

    // 保存退回的旧件
    @Override
    public ZjWorkSpareReturn save(AccounLoginInfo loginInfo, Long workId, String workNo, String workSpareNo, Integer workSpareStatus, Long networkId, WorkSpareCheckVo workSpareCheckVo) {
        if (workSpareCheckVo.getOldCopeWay() == 1) {
//            WorkServiceLog.saveService(new WorkLogVo(loginInfo, workId, "工单备件审核并处理旧件", "旧件处理方式：【不回收处理】"));
            return null;
        }
        if (workSpareStatus == WorkConstant.WORK_SPARE_STATUS_3) {
//            WorkServiceLog.saveService(new WorkLogVo(loginInfo, workId, "工单备件审核并处理旧件",
//                    "工单备件的旧件处理方式：【旧件退回】；备注：没有生成旧件回收单，此旧件回收单根据备件物料状态【审核通过】的备件生成，此备件物料已全部驳回，因此无法生成旧件回收单"));
            return null;
        }
        ZjWorkSpareReturn spareReturn = new ZjWorkSpareReturn();
        spareReturn.setId(workSpareCheckVo.getId());
        spareReturn.setCustAccId(loginInfo.getCustAccId());
        spareReturn.setWorkId(workId);
        spareReturn.setWorkNo(workNo);
        spareReturn.setWorkSpareNo(workSpareNo);
        spareReturn.setNetworkId(networkId);
        spareReturn.setCode(ZbOrderNoUtil.orderOldSpareNo(loginInfo.getCustAccId()));
        spareReturn.setStatus(WorkConstant.WORK_SPARE_RETURN_1);
        spareReturn.setOutStore(false);
        spareReturn.setOutStoreType(1);
        spareReturn.setStoreId(workSpareCheckVo.getStoreId());
        spareReturn.setStorePositionId(workSpareCheckVo.getStorePositionId());
        this.save(spareReturn);

        ZjWorkSpareReturnReceiptSend returnReceiptSend = new ZjWorkSpareReturnReceiptSend();
        returnReceiptSend.setId(spareReturn.getId());
        returnReceiptSend.setCustAccId(loginInfo.getCustAccId());
        returnReceiptSend.setReceiptAddrType(workSpareCheckVo.getReceiptAddrType());
        returnReceiptSend.setUserOrNetworkId(workSpareCheckVo.getUserOrNetworkId());
        returnReceiptSend.setSendName(workSpareCheckVo.getReceiptName());
        returnReceiptSend.setSendPhone(workSpareCheckVo.getReceiptPhone());
        returnReceiptSend.setSendProvince(workSpareCheckVo.getReceiptProvince());
        returnReceiptSend.setSendCity(workSpareCheckVo.getReceiptCity());
        returnReceiptSend.setSendArea(workSpareCheckVo.getReceiptArea());
        returnReceiptSend.setSendTown(workSpareCheckVo.getReceiptTown());
        returnReceiptSend.setSendAddress(workSpareCheckVo.getReceiptAddress());
        returnReceiptSend.setReceiptName(workSpareCheckVo.getOldReceiptName());
        returnReceiptSend.setReceiptPhone(workSpareCheckVo.getOldReceiptPhone());
        returnReceiptSend.setReceiptProvince(workSpareCheckVo.getOldReceiptProvince());
        returnReceiptSend.setReceiptCity(workSpareCheckVo.getOldReceiptCity());
        returnReceiptSend.setReceiptArea(workSpareCheckVo.getOldReceiptArea());
        returnReceiptSend.setReceiptTown(workSpareCheckVo.getOldReceiptTown());
        returnReceiptSend.setReceiptAddress(workSpareCheckVo.getOldReceiptAddress());
        workSpareReturnReceiptSendService.save(returnReceiptSend);

        List<ZjWorkSpareReturnMaterial> materials = workSpareCheckVo.getWorkSpareMaterials().stream().filter(f -> f.getStatus() == 2).map(vo -> {
            ZjWorkSpareReturnMaterial material = new ZjWorkSpareReturnMaterial();
            material.setId(IdWorker.getId());
            material.setCustAccId(loginInfo.getCustAccId());
            material.setWorkSpareReturnId(spareReturn.getId());
            material.setSpareId(vo.getSpareId());
            material.setSpecId(vo.getSpecId());
            material.setQty(vo.getQty());
            return material;
        }).toList();
        workSpareReturnMaterialService.saveBatch(materials);

//        WorkServiceLog.saveService(new WorkLogVo(loginInfo, workId, "工单备件审核并处理旧件", "工单备件的旧件处理方式：【旧件退回】"));
        WorkServiceLog.saveServiceWorkSpareOldLog(new WorkSpareLogVo(loginInfo, workId, returnReceiptSend.getId(), "工单备件审核并处理旧件", "工单备件的旧件处理方式：【旧件退回】"));
        return spareReturn;
    }

    // 获取工单备件旧件退回列表
    @Override
    public List<ZjWorkSpareReturn> getList(Long custAccId, WorkSpareReturnSelectVo selectVo) {
        return workSpareReturnMapper.getList(custAccId, selectVo);
    }

    // 获取工单备件旧件退回列表总记录数
    @Override
    public long getListCount(Long custAccId, WorkSpareReturnSelectVo selectVo) {
        return workSpareReturnMapper.getListCount(custAccId, selectVo);
    }

    // 根据仓库ID获取关联工单备件旧件关联的仓库ID
    @Override
    public List<Long> getRelationByStoreIds(List<Long> storeIds) {
        if (CollectionUtil.isEmpty(storeIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkSpareReturn> wrapper = new QueryWrapper<>();
        wrapper.select("store_id");
        wrapper.in("store_id", storeIds);
        return this.list(wrapper).stream().map(ZjWorkSpareReturn::getStoreId).distinct().toList();
    }

}
